package com.applet.service.impl;

import com.applet.common.constant.StateConstant;
import com.applet.common.context.BaseContext;
import com.applet.mapper.VideoMapper;
import com.applet.pojo.dto.PublishVideoDTO;
import com.applet.pojo.dto.UpdateVideoDTO;
import com.applet.pojo.entity.Video;
import com.applet.pojo.vo.VideoVO;
import com.applet.service.VideoService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

/**
 * @author “XuShao”
 * @date 2024/12/17 18:34
 * @description
 */
@Service
public class VideoServiceImpl implements VideoService {

    @Autowired
    private VideoMapper videoMapper;

    @Override
    public void addVideo(PublishVideoDTO publishVideoDTO) {
        Video video = new Video();

        BeanUtils.copyProperties(publishVideoDTO, video);

        video.setIssueId(BaseContext.getCurrentId());
        video.setIssueTime(LocalDateTime.now());
        video.setUpdateTime(LocalDateTime.now());
        video.setDeleted(StateConstant.ENABLE);

        videoMapper.insert(video);
    }

    @Override
    public void updateVideo(UpdateVideoDTO updateVideoDTO) {
        Video video = new Video();
        BeanUtils.copyProperties(updateVideoDTO, video);

        video.setUpdateTime(LocalDateTime.now());

        videoMapper.updateById(video);
    }

    @Override
    public List<VideoVO> getAllVideo() {
        List<Video> videos = videoMapper.selectList(null);

        List<VideoVO> videoVOS = new ArrayList<>();

        for(Video item : videos) {
            VideoVO videoVO = new VideoVO();
            BeanUtils.copyProperties(item, videoVO);
            videoVOS.add(videoVO);
        }

        return videoVOS;
    }

    @Override
    public void deleteVideo(String id) {
        videoMapper.deleteById(id);
    }
}
